﻿Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Reflection
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities.Rules
    <Serializable> _
    Public Class RuleUpdateAction
        Inherits RuleAction
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal path As String)
            Me.path = path
        End Sub

        Public Overrides Function Clone() As RuleAction
            Return DirectCast(MyBase.MemberwiseClone, RuleAction)
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim action As RuleUpdateAction = [TryCast](obj,RuleUpdateAction)
            Return ((Not action Is Nothing) AndAlso String.Equals(Me.Path, action.Path, StringComparison.Ordinal))
        End Function

        Public Overrides Sub Execute(ByVal context As RuleExecution)
        End Sub

        Public Overrides Function GetHashCode() As Integer
            Return MyBase.GetHashCode
        End Function

        Public Overrides Function GetSideEffects(ByVal validation As RuleValidation) As ICollection([Of] String)
            Return New String() { Me.path }
        End Function

        Public Overrides Function ToString() As String
            Return ("Update(""" & Me.path & """)")
        End Function

        Public Overrides Function Validate(ByVal validator As RuleValidation) As Boolean
            If (validator Is Nothing) Then
                Throw New ArgumentNullException("validator")
            End If
            Dim flag As Boolean = True
            If (Me.path Is Nothing) Then
                Dim error As New ValidationError(Messages.NullUpdate, &H53D)
                [error].UserData.Item("ErrorObject") = Me
                validator.AddError([error])
                flag = False
            End If
            Dim strArray As String() = Me.path.Split(New Char() { "/"c })
            If (strArray(0) = "this") Then
                Dim thisType As Type = validator.ThisType
                Dim i As Integer
                For i = 1 To strArray.Length - 1
                    If (strArray(i) = "*") Then
                        If (i < (strArray.Length - 1)) Then
                            Dim error2 As New ValidationError(Messages.InvalidWildCardInPathQualifier, &H195)
                            error2.UserData.Item("ErrorObject") = Me
                            validator.AddError(error2)
                            flag = False
                        End If
                        Return flag
                    End If
                    If (Not String.IsNullOrEmpty(strArray(i)) OrElse (i <> (strArray.Length - 1))) Then
                        goto Label_00ED
                    End If
                    Return flag
                Label_00E6:
                    thisType = thisType.GetElementType
                Label_00ED:
                    If thisType.IsArray Then
                        goto Label_00E6
                    End If
                    Dim bindingAttr As BindingFlags = (BindingFlags.FlattenHierarchy Or (BindingFlags.Public Or (BindingFlags.Static Or BindingFlags.Instance)))
                    If validator.AllowInternalMembers(thisType) Then
                        bindingAttr = (bindingAttr Or BindingFlags.NonPublic)
                    End If
                    Dim field As FieldInfo = thisType.GetField(strArray(i), bindingAttr)
                    If (Not field Is Nothing) Then
                        thisType = field.FieldType
                    Else
                        Dim property As PropertyInfo = thisType.GetProperty(strArray(i), bindingAttr)
                        If (Not [property] Is Nothing) Then
                            thisType = [property].PropertyType
                        Else
                            Dim error3 As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, New Object() { strArray(i) }), &H57B)
                            error3.UserData.Item("ErrorObject") = Me
                            validator.AddError(error3)
                            Return False
                        End If
                    End If
                Next i
                Return flag
            End If
            Dim error4 As New ValidationError(Messages.UpdateNotThis, &H57B)
            error4.UserData.Item("ErrorObject") = Me
            validator.AddError(error4)
            Return False
        End Function


        ' Properties
        Public Property Path As String
            Get
                Return Me.path
            End Get
            Set(ByVal value As String)
                Me.path = value
            End Set
        End Property


        ' Fields
        Private path As String
    End Class
End Namespace

